In [45]:
from pylab import *

t = linspace(-pi, pi, 80)
x = 16 * sin(t) * sin(t) * sin(t)
y = 13 * cos(t) - 5 * cos(2*t) - 2 * cos(3*t) - cos(4*t)

plot(x, y)
title('Heart Curve')
show()


while

while exp: # 你要执行的代码 pass

In [ ]:
i = 0
while i < 5:
    print('i == %d' % i)
    i += 1

无限循环


In [ ]:
while True:
    wait_for_connect()
    process_request()
    send_data_back()

for

for var in iterables: # 你要执行的代码 pass

In [ ]:
for i in 'python is fun':
    print(i)

In [ ]:
for i in ['ball', 'box', 'cube']:
    print(i)

In [ ]:
nameList = ['ball', 'box', 'cube']
for i, name in enumerate(nameList):
    print('%d %s' % (i, name))

In [ ]:
range(0, 10, 1)

In [ ]:
range(10, 0, -1)

In [ ]:
range(0, 10, 2)

In [ ]:
range(10)

In [ ]:
xrange(10)

In [ ]:
for i in range(5):
    print('i == %d' % i)

In [ ]:
for i in xrange(5):
    print('i == %d' % i)

In [ ]:
for i in range(5, 10):
    print('i == %d' % i)

break


In [ ]:
i = 0
while i < 10:
    if i == 5:
        break
    print('i == %d' % i)
    i += 1

In [ ]:
for i in range(10):
    if i == 5:
        break
    print('i == %d' % i)

continue


In [ ]:
i = 0
while i < 10:
    if i == 5:
        i += 1
        continue
    print('i == %d' % i)
    i += 1

In [ ]:
for i in range(10):
    if i == 5:
        continue
    print('i == %d' % i)

In [ ]:
for i in range(10):
    if i == 5:
        pass
    print('i == %d' % i)

In [ ]:
i = 0
while i < 5:
    print('i == %d' % i)
    i += 1
else:
    i -= 10
    print('i == %d' % i)

迭代器


In [ ]:
nameList

In [ ]:
nameIt = iter(nameList)
nameIt.next()

In [ ]:
nameIt.next()

In [ ]:
for i in iter(nameList):
    print(i)

列表解析


In [ ]:
seq = []
for i in range(10):
    seq.append(i + 2)
seq

In [ ]:
[i + 2 for i in range(10)]

In [ ]:
[(x+1, y+1) for x in range(3) for y in range(3)]

In [ ]:
xy = []
for x in range(3):
    for y in range(3):
        xy.append((x+1, y+1))
xy

In [ ]:
[i for i in range(10) if i % 2]

In [ ]:
# --------------------------------------------------------------------------------  
# Copyright (c) 2013 - 2014 Mack Stone. All rights reserved.  
#   
# Permission is hereby granted, free of charge, to any person obtaining a copy  
# of this software and associated documentation files (the "Software"), to deal  
# in the Software without restriction, including without limitation the rights  
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
# copies of the Software, and to permit persons to whom the Software is  
# furnished to do so, subject to the following conditions:  
#   
# The above copyright notice and this permission notice shall be included in  
# all copies or substantial portions of the Software.  
#   
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN  
# THE SOFTWARE.  
# --------------------------------------------------------------------------------